
package com.huifaapp.mall.view.titlebar;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

@SuppressLint({"NewApi"})
public class ActionTitleBarWidget extends LinearLayout implements OnClickListener {
    private LinearLayout tb_view;
    private LinearLayout leftView;
    private LinearLayout centerView;
    private LinearLayout rightView;
    private Context context;
    private ActionTitleBarWidget.ClickListener clickListener;
    private DisplayMetricsTool displayMetricsTool;
    private ActionTitleBarWidget.addTextChanged textChanged;
    private ActionTitleBarWidget.SearchEditOnClickListener searchEditOnClickListener;
    private ActionTitleBarWidget.OnTabCheckedChangeListener checkedChangeListener;
    RadioButton button1;
    RadioButton button3;//asd

    public ActionTitleBarWidget(Context context) {
        super(context);
        this.context = context;
    }

    public ActionTitleBarWidget(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        this.initView();
    }

    private void initView() {
        this.displayMetricsTool = new DisplayMetricsTool((Activity)this.getContext());
        this.tb_view = new LinearLayout(this.getContext());
        this.leftView = new LinearLayout(this.getContext());
        this.centerView = new LinearLayout(this.getContext());
        this.rightView = new LinearLayout(this.getContext());
        this.tb_view.addView(this.leftView, 0);
        this.tb_view.addView(this.centerView, 1);
        this.tb_view.addView(this.rightView, 2);
        this.setLayoutParamsView(this.tb_view, DisplayMetricsTool.getWidth(), DensityTool.dip2px(this.getContext(), 48.0F));
        this.leftView.setLayoutParams(new LayoutParams(DisplayMetricsTool.getWidth() / 3, -1));
        this.centerView.setLayoutParams(new LayoutParams(DisplayMetricsTool.getWidth() - DisplayMetricsTool.getWidth() / 3 * 2, -1));
        this.rightView.setLayoutParams(new LayoutParams(DisplayMetricsTool.getWidth() / 3, -1));
        this.rightView.setPadding(0, 0, 26, 0);
        this.leftView.setOnClickListener(this);
        this.rightView.setOnClickListener(this);
        this.centerView.setOnClickListener(this);
        this.setRightGravity(21);
        this.addView(this.tb_view);
    }

    private void setLayoutParamsView(View view, int width, int height) {
        view.setLayoutParams(new LayoutParams(width, height));
    }

    public void setLeftText(String text, int textsize, int color) {
        leftView.removeAllViews();
        TextView temp = new TextView(this.getContext());
        temp.setText(text);
        temp.setTextColor(color);
        if(textsize != 0) {
            temp.setTextSize((float)textsize);
        }

        this.leftView.addView(temp);
    }

    public void setRightText(String text, int textsize, int color) {
        rightView.removeAllViews();
        TextView temp = new TextView(this.getContext());
        temp.setText(text);
        temp.setTextColor(color);
        if(textsize != 0) {
            temp.setTextSize((float)textsize);
        }

        this.rightView.addView(temp);
    }

    public void setLeftIco(int resId) {
        leftView.removeAllViews();
        ImageView temp = new ImageView(this.getContext());
        temp.setImageResource(resId);
        LayoutParams params = new LayoutParams(-2, -2);
        params.setMargins(0, DensityTool.dip2px(this.getContext(), 5.0F), 0, DensityTool.dip2px(this.getContext(), 5.0F));
        temp.setLayoutParams(params);
        this.leftView.addView(temp);
    }

    public void setTitleBackgroundResource(int leftresid, int rightresid) {

        if(leftresid != 0) {
            this.leftView.setBackgroundResource(leftresid);
        }

        if(rightresid != 0) {
            this.rightView.setBackgroundResource(rightresid);
        }

    }

    public void setRightIco(int resId) {
        rightView.removeAllViews();
        ImageView temp = new ImageView(this.getContext());
        temp.setImageResource(resId);
        LayoutParams params = new LayoutParams(-2, -2);
        params.setMargins(0, DensityTool.dip2px(this.getContext(), 5.0F), 0, DensityTool.dip2px(this.getContext(), 5.0F));
        temp.setLayoutParams(params);
        this.rightView.addView(temp);
    }

    public void setCenterText(String text, int textsize, int color) {
        centerView.removeAllViews();
        TextView temp = new TextView(this.getContext());
        temp.setText(text);
        temp.setTextColor(color);
        if(textsize != 0) {
            temp.setTextSize((float)textsize);
        }

        this.centerView.addView(temp);
    }

    public void setCenterIco(int resId) {
        centerView.removeAllViews();
        ImageView temp = new ImageView(this.getContext());
        temp.setImageResource(resId);
        LayoutParams params = new LayoutParams(-2, -2);
        params.setMargins(0, DensityTool.dip2px(this.getContext(), 5.0F), 0, DensityTool.dip2px(this.getContext(), 5.0F));
        temp.setLayoutParams(params);
        this.centerView.addView(temp);
    }

    public void setCenterView(View view) {
        this.centerView.addView(view);
    }

    public void setRightView(View view) {
        this.rightView.addView(view);
    }

    public void setLeftView(View view) {
        this.leftView.addView(view);
    }

    public void setCenterGravity(int gravity) {
        this.centerView.setGravity(gravity);
    }

    public void setRightGravity(int gravity) {
        this.rightView.setGravity(21);
    }

    public void setRightEnabled(boolean b) {
        this.rightView.setEnabled(b);
    }

    public void setLeftGravity(int gravity) {
        this.leftView.setGravity(gravity);
    }

    public void setTitleBarBackgroundColor(int color) {
        this.tb_view.setBackgroundColor(color);
    }

    public void setTitleBarBackgroundAlpha(float color) {
        this.tb_view.setAlpha(color);
    }

    public void setTitleBarBackgroundImage(int resid) {
        this.tb_view.setBackgroundResource(resid);
    }

    public void setLeftPadding(int left, int top, int right, int bottom) {
        this.leftView.setPadding(left, top, right, bottom);
    }

    public void setRightPadding(int left, int top, int right, int bottom) {
        this.rightView.setPadding(left, top, right, bottom);
    }

    public void setCenterPadding(int left, int top, int right, int bottom) {
        this.centerView.setPadding(left, top, right, bottom);
    }

    public void OnTitleBarClickListener(ActionTitleBarWidget.ClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public void onClick(View v) {
        if(v == this.leftView) {
            if(v != null && this.clickListener != null) {
                this.clickListener.onleft(v);
            }
        } else if(v == this.rightView) {
            if(v != null && this.clickListener != null) {
                this.clickListener.onright(v);
            }
        } else if(v == this.centerView && v != null && this.clickListener != null) {
            this.clickListener.oncenter(v);
        }

    }

    public View getSearchEditTextView(String hint, int Width, int textcolor, int hintimg, int bg, boolean isEnabled, boolean isFocusable, final int state) {
        final EditText editText = new EditText(this.context);
        Drawable drawable = this.getResources().getDrawable(hintimg);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        editText.setCompoundDrawables(drawable, (Drawable)null, (Drawable)null, (Drawable)null);
        editText.setGravity(17);
        editText.setHint(hint);
        editText.setHintTextColor(textcolor);
        editText.setTextColor(textcolor);
        editText.setTextSize(15.0F);
        editText.setPadding(DensityTool.dip2px(this.context, 10.0F), 0, DensityTool.dip2px(this.context, 10.0F), 0);
        editText.setBackgroundResource(bg);
        editText.setInputType(1);
        editText.setLayoutParams(new LayoutParams(DisplayMetricsTool.getWidth() - (DisplayMetricsTool.getWidth() / Width + 50), DensityTool.dip2px(this.getContext(), 37.0F)));
        editText.setEnabled(isEnabled);
        editText.setFocusable(isFocusable);
        editText.addTextChangedListener(new TextWatcher() {
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                ActionTitleBarWidget.this.textChanged.TextChanged(editText.getText().toString().trim(), s, start, count);
            }

            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            public void afterTextChanged(Editable s) {
            }
        });
        editText.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if(v != null && state == 1) {
                    ActionTitleBarWidget.this.searchEditOnClickListener.onClick(v);
                }

            }
        });
        return editText;
    }

    public void setSearchEditOnClickListener(ActionTitleBarWidget.SearchEditOnClickListener searchEditOnClickListener) {
        this.searchEditOnClickListener = searchEditOnClickListener;
    }

    public View getTABView(int textcolor, int layoutbg, int Width, String text1, String text2, String text3, int bg1, int bg2, int bg3) {
        LinearLayout layout = new LinearLayout(this.context);
        layout.setLayoutParams(new LayoutParams(DisplayMetricsTool.getWidth() - (DisplayMetricsTool.getWidth() / Width + 50), DensityTool.dip2px(this.getContext(), 37.0F)));
        layout.setBackgroundResource(layoutbg);
        RadioGroup group = new RadioGroup(this.context);
        group.setOrientation(2);
        this.button1 = new RadioButton(this.context);
        this.button3 = new RadioButton(this.context);
        this.button1.setGravity(17);
        this.button3.setGravity(17);
        this.button1.setTextColor(this.getResources().getColorStateList(textcolor));
        this.button3.setTextColor(this.getResources().getColorStateList(textcolor));
        this.button1.setBackgroundResource(bg1);
        this.button3.setBackgroundResource(bg3);
        this.button1.setButtonDrawable(new ColorDrawable(0));
        this.button3.setButtonDrawable(new ColorDrawable(0));
        this.button1.setPadding(0, 0, 0, 0);
        this.button3.setPadding(0, 0, 0, 0);
        LayoutParams layoutParams = new LayoutParams((DisplayMetricsTool.getWidth() - (DisplayMetricsTool.getWidth() / Width + 50)) / 2, DensityTool.dip2px(this.getContext(), 37.0F));
        this.button1.setLayoutParams(layoutParams);
        this.button3.setLayoutParams(layoutParams);
        this.button1.setText(text1);
        this.button3.setText(text3);
        group.addView(this.button1, 0);
        group.addView(this.button3, 1);
        layout.addView(group);
        this.button1.setChecked(true);
        group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(ActionTitleBarWidget.this.button1.getId() == checkedId) {
                    ActionTitleBarWidget.this.checkedChangeListener.button1();
                } else if(ActionTitleBarWidget.this.button3.getId() == checkedId) {
                    ActionTitleBarWidget.this.checkedChangeListener.button3();
                }

            }
        });
        return layout;
    }

    public void CenterViewRemove() {
        if(this.centerView != null) {
            this.centerView.removeAllViews();
        }

    }

    public void LeftViewRemove() {
        if(this.leftView != null) {
            this.leftView.removeAllViews();
        }

    }

    public void RightViewRemove() {
        if(this.rightView != null) {
            this.rightView.removeAllViews();
        }

    }

    public void onAddTextChanged(ActionTitleBarWidget.addTextChanged textChanged) {
        this.textChanged = textChanged;
    }

    public void OnTabCheckedChange(ActionTitleBarWidget.OnTabCheckedChangeListener checkedChangeListener) {
        this.checkedChangeListener = checkedChangeListener;
    }

    public interface ClickListener {
        void onleft(View var1);

        void onright(View var1);

        void oncenter(View var1);
    }

    public interface OnTabCheckedChangeListener {
        void button1();

        void button3();
    }

    public interface SearchEditOnClickListener {
        void onClick(View var1);
    }

    public interface addTextChanged {
        void TextChanged(String var1, CharSequence var2, int var3, int var4);
    }
    public  int getStatusBarHeight(Context context) {
        int result = 0;
        if(Build.VERSION.SDK_INT>19) {
            int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
            if (resourceId > 0) {
                result = context.getResources().getDimensionPixelSize(resourceId);
            }
        }
        return result;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

}
